Grokking JavaScript Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who should take this course?
Course Overview
Introduction to JavaScript
JavaScript Basics
JavaScript - Hello World
JavaScript - Comments
Quiz
JavaScript Variables and Operators
JavaScript - Variables
JavaScript - Operators
Quiz
JavaScript Conditionals
Introduction to Conditional Statements in JavaScript
JavaScript - If...Else
JavaScript - Switch Case
Quiz
JavaScript Objects
Introduction to Objects in JavaScript
JavaScript - Number
JavaScript - Boolean
JavaScript - Math
JavaScript - Date
Quiz
Introductions to Function in JavaScript
JavaScript - Variable Scope
JavaScript - Default Parameters
JavaScript - Arrow Functions
Quiz
Introduction to Strings in JavaScript
JavaScript - String Methods
Introduction to Array in JavaScript
JavaScript - Array Methods
Quiz
Introduction to loops in JavaScript
JavaScript - for loop
JavaScript - while loop
JavaScript - do-while loop
JavaScript - for-in loop
JavaScript - for-of loop
JavaScript - Loop Control
Quiz
JavaScript - Maps
JavaScript - Sets
Quiz
Where to Go from Here
JavaScript - Date

The Date object in JavaScript is used to work with dates and times. It allows developers to get the current date and time, create specific dates, and manipulate date data, which is crucial for applications that rely on temporal information, such as calendars, reminders, and time tracking tools.

Syntax

The JavaScript Date object can be instantiated using various syntaxes, each serving different purposes based on the parameters provided. Here's a closer look at each syntax:

new Date() new Date(milliseconds) new Date(datestring) new Date(year,month,date, [hour,minute,second,millisecond ])
  1. new Date()

    • Creates a new Date object with the current date and time.
    • No parameters are passed.
  2. new Date(milliseconds)

    • Creates a Date object based on the number of milliseconds since January 1, 1970, UTC.
    • Useful for creating dates from timestamps.
  3. new Date(dateString)

    • Accepts a date string (e.g., "March 25, 2015") and creates a Date object representing the specified date and time.
    • The string format should be recognized by the Date.parse() method.
  4. new Date(year, month, date[, hour, minute, second, millisecond ])

    • Creates a Date object with the specified date and time. year and month parameters are required, while the rest are optional.
    • month is 0-indexed (0 for January, 11 for December).
    • If only year and month are provided, the day defaults to the first of the month.
    • Times default to 0 if not provided.

Date Methods

Here are some essential Date methods grouped by their functionality:

MethodDescription
getDate()Returns the day of the month (1-31).
getDay()Returns the day of the week (0-6).
getFullYear()Returns the year.
getHours()Returns the hour (0-23).
getMilliseconds()Returns the milliseconds (0-999).
getMinutes()Returns the minutes (0-59).
getMonth()Returns the month (0-11).
getSeconds()Returns the seconds (0-59).
getTime()Returns the number of milliseconds since Jan 1, 1970.
getTimezoneOffset()Returns the time difference between UTC and Local Time in minutes.
getUTCDate()Returns the day of the month (UTC) (1-31).
(And many more UTC methods corresponding to the local time methods)
setDate()Sets the day of the month.
setFullYear()Sets the year.
setHours()Sets the hour.
setMilliseconds()Sets the milliseconds.
setMinutes()Sets the minutes.
setMonth()Sets the month.
setSeconds()Sets the seconds.
setTime()Sets the time (milliseconds since Jan 1, 1970).
(And corresponding setUTC... methods)
toDateString()Converts the date portion to a readable string.
toISOString()Returns the ISO string format of the date.
toLocaleDateString()Returns the date portion in a locale-specific format.
toString()Converts the Date object to a string.
toTimeString()Converts the time portion of a Date object to a string.
valueOf()Returns the primitive value of a Date object.

Examples

Example 1: Getting the Current Date and Time

Javascript

. . . .
  • This creates a new Date object for the current date and time and outputs it as a string.

Example 2: Setting and Getting Specific Date Components

Javascript

. . . .
  • Here, we modify the year, month, and day of a Date object to represent a specific date (my birthday) and then output it in a locale-specific format.

Example 3: Calculating the Difference Between Two Dates

Javascript

. . . .
  • Calculates the difference in days between two dates by subtracting their timestamps and converting the result from milliseconds to days.
JavaScript - Math
Quiz
Mark as Completed

On this page

Syntax

Date Methods

Examples